package com.link2room.rodimus.facade.impl;

import com.link2room.common.constant.DefaultValue;
import com.link2room.common.util.L2RAssert;
import com.link2room.rodimus.constant.SignInConstant;
import com.link2room.rodimus.dto.param.base.SignInBaseDto;
import com.link2room.rodimus.dto.param.base.SignInReissueHandlerParamDto;
import com.link2room.rodimus.dto.param.base.SignInReissueRuleResvDto;
import com.link2room.rodimus.facade.IAfterPMSdepDoFacadeService;
import com.link2room.rodimus.handler.ISignInDrawReissueHandler;
import com.link2room.rodimus.service.ISignInService;
import com.link2room.rodimus.util.CalendarUtils;
import com.link2room.soundwave.dto.param.inside.PmsReservationInsideParamDto;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by LN on 2018/1/19
 */
@Component("signInReissueDepDoFacadeService")
public class SignInReissueDepDoFacadeServiceImpl implements IAfterPMSdepDoFacadeService {
    @Resource
    private ISignInService signInService;
    @Resource
    private ISignInDrawReissueHandler signInDrawReissueHandler;

    @Override
    public void afterPMSDepDo(PmsReservationInsideParamDto pmsReservationInsideParamDto) throws Exception {
        String l2rNo = pmsReservationInsideParamDto.getL2RNo();
        L2RAssert.stringBlank(l2rNo, "[l2rNo] is required.");
        String memberNo = pmsReservationInsideParamDto.getMemberNo();
        L2RAssert.stringBlank(memberNo, "[memberNo] is required.");
        String lhotelGroupCode = pmsReservationInsideParamDto.getLhotelGroupCode();

        SignInBaseDto signInBase = signInService.getUnderwaySignInBaseAll(lhotelGroupCode);
        if (null == signInBase) {
            return;
        }
        String couldReissue = signInBase.getCouldReissue();
        if (!DefaultValue.T.equals(couldReissue)) {
            return;
        }

        SignInReissueRuleResvDto resvRule = signInBase.getSignInReissueRuleResv();
        if (null == resvRule) {
            return;
        }
        Integer value = resvRule.getValue();
        if (value == null || value < 1) {
            value = 1;
        }
        Date nowMonth;

        Date dep = pmsReservationInsideParamDto.getDep();
        if (dep == null) {
            nowMonth = CalendarUtils.getStartTimeOfMonth(new Date());
        } else {
            nowMonth = CalendarUtils.getStartTimeOfMonth(dep);
        }

        SignInReissueHandlerParamDto paramDto = new SignInReissueHandlerParamDto();
        paramDto.setMemberNo(memberNo);
        paramDto.setL2rNo(l2rNo);
        paramDto.setValue(value);
        paramDto.setMonth(nowMonth);
        paramDto.setLhotelGroupCode(lhotelGroupCode);
        signInDrawReissueHandler.signInReissue(SignInConstant.ReissueType.RESV, paramDto);


    }
}
